from tortoise.models import Model
from tortoise import fields
from datetime import datetime


class Sale(Model):
    """
    销售订单模型
    """
    saleid = fields.CharField(max_length=18, pk=True, description="销售单号")
    type = fields.CharField(max_length=20, null=True, description="订单类型")
    cusid = fields.IntField(description="客户ID")
    cusname = fields.CharField(max_length=100, null=True, description="客户名称")
    owner = fields.IntField(description="经办人")
    whid = fields.IntField(description="仓库ID")
    orderamount = fields.DecimalField(max_digits=10, decimal_places=2, default=0.00, description="订单金额")
    whamount = fields.DecimalField(max_digits=10, decimal_places=2, default=0.00, description="入库金额")
    yjamount = fields.DecimalField(max_digits=10, decimal_places=2, default=0.00, description="已结款")
    wjamount = fields.DecimalField(max_digits=10, decimal_places=2, default=0.00, description="未结款")
    statu = fields.CharField(max_length=30, description="状态")
    shtime = fields.DatetimeField(description="审核时间")
    remark = fields.CharField(max_length=100, null=True, description="备注")
    del_flag = fields.CharField(max_length=1, default="0", description="删除标志(0代表存在 2代表删除)")
    create_by = fields.CharField(max_length=64, default="", description="创建者")
    create_time = fields.DatetimeField(null=True, description="创建时间")
    update_by = fields.CharField(max_length=64, default="", description="更新者")
    update_time = fields.DatetimeField(null=True, description="更新时间")

    class Meta:
        table = "wms_sale"
        table_description = "销售订单表"

    def __str__(self):
        return f"Sale(saleid={self.saleid}, cusname={self.cusname})"


class SaleItem(Model):
    """
    销售订单商品项模型
    """
    itemid = fields.IntField(pk=True, description="销售项ID")
    saleid = fields.CharField(max_length=18, null=True, description="销售单号")
    orderid = fields.IntField(null=True, description="序号")
    pid = fields.IntField(null=True, description="商品ID")
    pname = fields.CharField(max_length=50, null=True, description="商品名称")
    unit = fields.CharField(max_length=20, null=True, description="单位")
    saletype = fields.CharField(max_length=20, null=True, description="业务属性")
    unitprice = fields.DecimalField(max_digits=10, decimal_places=2, null=True, description="销售单价")
    number = fields.DecimalField(max_digits=10, decimal_places=2, null=True, description="数量")
    orderamount = fields.DecimalField(max_digits=10, decimal_places=2, null=True, description="金额")
    whnumber = fields.DecimalField(max_digits=10, decimal_places=2, default=0, description="出库数量")
    whamount = fields.DecimalField(max_digits=10, decimal_places=2, null=True, description="出库金额")
    whid = fields.IntField(null=True, description="仓库ID")
    whname = fields.CharField(max_length=20, null=True, description="仓库名称")
    remark = fields.CharField(max_length=255, null=True, description="备注")
    del_flag = fields.CharField(max_length=1, default="0", description="删除标志(0代表存在 2代表删除)")
    create_by = fields.CharField(max_length=64, default="", description="创建者")
    create_time = fields.DatetimeField(null=True, description="创建时间")
    update_by = fields.CharField(max_length=64, default="", description="更新者")
    update_time = fields.DatetimeField(null=True, description="更新时间")

    class Meta:
        table = "wms_sale_item"
        table_description = "销售订单商品项表"

    def __str__(self):
        return f"SaleItem(itemid={self.itemid}, pname={self.pname})"